package com.googleplacesandmaps;

import java.util.Date;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class FrontPage extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_front_page);

		EditText type = (EditText) findViewById(R.id.type);
		EditText name = (EditText) findViewById(R.id.name);

		type.setOnEditorActionListener(new TextView.OnEditorActionListener() {
		    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
		            search();
		            return true;
		        }
		        return false;
		    }
		});
		
		name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
		    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
		        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
		            search();
		            return true;
		        }
		        return false;
		    }
		});
		
		Button btn_search = (Button) findViewById(R.id.btn_search);
		btn_search.setOnClickListener(new View.OnClickListener() {

			public void onClick(View V) {
				search();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_front_page, menu);
		return true;
	}

	public void search() {
		EditText type = (EditText) findViewById(R.id.type);
		EditText name = (EditText) findViewById(R.id.name);
		TimePicker time = (TimePicker) findViewById(R.id.time);
		DatePicker date = (DatePicker) findViewById(R.id.date);
		String searchTime;

		time.setIs24HourView(true);

		if(type.getText().toString().trim().matches("") && name.getText().toString().trim().matches("")){
			Toast.makeText(FrontPage.this, "Please enter the NAME or TYPE of place", Toast.LENGTH_LONG).show();
			return;
		}
		
		if (time.getCurrentMinute() > 10)
			searchTime = time.getCurrentHour().toString() + ":"
					+ time.getCurrentMinute().toString();
		else
			searchTime = time.getCurrentHour().toString() + ":0"
					+ time.getCurrentMinute().toString();

		time.setIs24HourView(false);

		String dayOfWeek = DateFormat.format(
				"EEEE",
				new Date(date.getYear(), date.getMonth(),
						date.getDayOfMonth() - 1)).toString();

		Intent intent = new Intent(FrontPage.this, MainActivity.class);
		intent.putExtra("names", name.getText().toString());
		intent.putExtra("types", type.getText().toString());
		intent.putExtra("time", searchTime);
		intent.putExtra("day", dayOfWeek);

        type.setText("");
        name.setText("");
        
        startActivity(intent);

	}

}
